package com.xh.bussiness.system.service.impl;

import com.xh.bussiness.system.bean.dto.IconDto;
import com.xh.bussiness.system.bean.request.IconRequest;
import com.xh.bussiness.system.model.Icon;
import com.xh.bussiness.system.dao.IconMapper;
import com.xh.bussiness.system.model.IconSubject;
import com.xh.bussiness.system.service.IIconService;
import com.xh.bussiness.system.service.IIconSubjectService;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.core.bean.XhPage;
import com.xh.core.util.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 图标 服务实现类
 * </p>
 *
 * @author zwb
 * @since 2021-06-03
 */
@Service
public class IconServiceImpl extends BaseServiceImpl<IconMapper, Icon> implements IIconService {

    @Autowired
    private IIconSubjectService subjectService;

    @Override
    public XhPage queryIconPage(XhPage page, IconRequest bean) {
        QueryWrapper qw = new QueryWrapper<Icon>();
        //拼装条件
        qw = prepareParams(bean, qw);
        return (XhPage) this.dao.selectPage(page, qw);
    }

    @Override
    public List queryIcon(IconRequest bean) {
        QueryWrapper qw = new QueryWrapper<Icon>();
        //拼装条件
        qw = prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }

    @Override
    public List<IconDto> queryIconBySubject(IconRequest bean) {
        List<IconDto> iconDtos = this.dao.queryIconBySubject(bean);
        return iconDtos;
    }

    @Override
    public XhPage<IconDto> queryIconPageBySubject(XhPage xhPage, IconRequest bean) {
        QueryWrapper<Icon> iconQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(bean.getSubject())){
            List<String> iconIds = subjectService.queryIconIdBySubject(bean.getSubject());
            if (iconIds.size() == 0) {
                return xhPage;
            }
            iconQueryWrapper.in("id", iconIds);
        }

//        iconQueryWrapper.select("id AS id", "css_type AS cssType",
//                "file_path AS filePath", "template_id AS templateId", "`status` AS `status`",
//                "type AS type", "modify_time AS modifyTime");
        iconQueryWrapper.eq("type", 2);
        iconQueryWrapper.orderByDesc("modify_time");
        xhPage = (XhPage) this.selectPage(xhPage, iconQueryWrapper, IconRequest.class);

        List<IconRequest> icons = xhPage.getRecords();
        for (IconRequest icon : icons) {
            List<IconSubject> subjects = subjectService.querySubjectNamesByIconId(icon.getId());
            icon.setIconSubjects(subjects);
        }

        return xhPage;
    }

    /**
     * 批量插入
     *
     * @param icons
     * @return boolean
     * @author 赵文博
     * @date 2021/6/4 15:08
     **/
    @Override
    public boolean batchInsertIcon(List<Icon> icons) {
        Integer index = 0;
        if (icons != null && icons.size() > 0) {
            index = this.dao.queryCount(icons.get(0));
        }
        if (index == null) {
            index = 0;
        }
        for (Icon icon : icons) {
            icon.setCssType(++index);
        }
        return this.batchInsert(icons);
    }

    /**
     * 添加图标
     *
     * @param iconRequests
     * @return boolean
     * @author 赵文博
     * @date 2021/6/24 16:41
     **/
    @Override
    @Transactional
    public boolean InsertIcon(List<IconRequest> iconRequests) {
        boolean flag = false;
        for (IconRequest iconRequest : iconRequests) {
            Icon icon = new Icon();
            BeanUtils.copyProperties(iconRequest, icon);
            Integer index = 0;
            index = this.dao.queryIconCount(iconRequest);
            icon.setCssType(++index);
            flag = this.insert(icon);
            if (flag) {
                List<String> subjects = iconRequest.getSubjects();
                List<String> subjectNames = iconRequest.getSubjectNames();
                List<IconSubject> iconSubjects = new ArrayList<>();
                for (int i = 0; i < subjects.size(); i++) {
                    IconSubject iconSubject = new IconSubject();
                    iconSubject.setIconId(icon.getId());
                    iconSubject.setSubject(subjects.get(i));
                    iconSubject.setSubjectName(subjectNames.get(i));
                    iconSubjects.add(iconSubject);
                }
                flag = subjectService.batchInsert(iconSubjects);
            }
        }
        return flag;

    }

    /**
     * @param iconRequests
     * @return boolean
     * @author 赵文博
     * @date 2021/6/25 11:41
     **/
    @Override
    public boolean updateIcon(List<IconRequest> iconRequests) {
        List<Icon> icons = new ArrayList<>();
        List<IconSubject> iconSubjects = new ArrayList<>();
        for (IconRequest iconRequest : iconRequests) {
            Icon icon = new Icon();
            BeanUtils.copyProperties(iconRequest, icon);
            icons.add(icon);

            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("icon_id", icon.getId());
            subjectService.deleteByCondition(queryWrapper);

            List<String> subjects = iconRequest.getSubjects();
            List<String> subjectNames = iconRequest.getSubjectNames();

            for (int i = 0; i < subjects.size(); i++) {
                IconSubject iconSubject = new IconSubject();
                iconSubject.setIconId(icon.getId());
                iconSubject.setSubject(subjects.get(i));
                iconSubject.setSubjectName(subjectNames.get(i));
                iconSubjects.add(iconSubject);
            }
        }
        boolean flag = false;
        if (icons.size() > 0) {
            this.batchUpdate(icons);
            flag = subjectService.batchInsert(iconSubjects);
        }
        return flag;
    }

    /**
     * 准备条件
     *
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(IconRequest bean, QueryWrapper qw) {
        if (bean.getIds() != null && bean.getIds().length > 0) {
            qw.in("id", bean.getIds());
        }
        if (bean.getCssType() != null) {
            qw.eq("css_type", bean.getCssType());
        }
        if (StringUtils.isNotEmpty(bean.getTemplateId())) {
            qw.eq("template_id", bean.getTemplateId());
        }
        if (bean.getSubjects() != null && bean.getSubjects().size() > 0) {
            qw.like("subjects", bean.getSubject());
        }
        if (bean.getType() != null) {
            qw.eq("type", bean.getType());
        }
        qw.eq("status", 1);
        qw.orderByAsc("css_type");
        return qw;
    }

    @Override
    public boolean off(String ids, Integer status) {
        String[] id = ids.split(",");
        return this.dao.off(id,status)>0;
    }
}
